package b3.widget.rank
{
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.net.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class RankingItem extends Sprite
	{
		private const RANK_COLOR:Array = [0xFF0000, 0xA0A000, 0x00A000, 0x000000];
		private const RANK_NAME:Array = ["st", "nd", "rd", ""];
		private var rankInfo:RankInfo;
		private var pictLdr:Loader;
		
		public function RankingItem(rankInfo:RankInfo)
		{
			super();
			this.rankInfo = rankInfo;
			//背景框
			var RankingItemBgr:Class = ResourceHelper.getInstance().getClassByName("RankingItemBgr");
			var rankingItemBgr:Sprite = new RankingItemBgr();
			addChild(rankingItemBgr);
			//用户头像
			if (rankInfo.figureurl != null)
			{
				pictLdr = new Loader();
				var pictURLReq:URLRequest = new URLRequest(rankInfo.figureurl);
				pictLdr.load(pictURLReq);
				pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onUserPicLoaded);
				addChild(pictLdr);
			}
			//用户名字
			var playerNameText:TextField = new TextField();
			playerNameText.selectable = false;
        	playerNameText.autoSize = TextFieldAutoSize.CENTER;
			playerNameText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000, true);
			playerNameText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			playerNameText.text = rankInfo.name.substr(0,4);
			playerNameText.x = (this.width - playerNameText.width) / 2;
			playerNameText.y = 65;
			addChild(playerNameText);
			//排名
			var NumFont:Class = ResourceHelper.getInstance().getClassByName("NumFont2");
			var rankSprite:Sprite = BFont.formatRank(rankInfo.ranking + 1, NumFont);
			rankSprite.scaleX = rankInfo.ranking < 3 ? 0.8 : 0.6;
			rankSprite.scaleY = rankSprite.scaleX;
			rankSprite.x = -10;
			rankSprite.y = 10;
			rankSprite.rotation = -25;
			addChild(rankSprite);
			
			this.mouseChildren = false;	
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onUserPicLoaded(event:Event):void
		{
			pictLdr.x = 43 - pictLdr.width / 2;
			pictLdr.y = 40 - pictLdr.height / 2;
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
		}

		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}
	}
}